import { get, set } from 'lodash';
import {WeaLocaleProvider} from "ecCom";
const getLabel = WeaLocaleProvider.getLabel;

export const mainTables = [];
export const detailIndex = {};
export const fieldIndex = {};
export const fieldName = {};
export const labels = {};



export function getMainTableFieldName(sheet, row, col, fieldIndex) {
    let label = ((sheet.getCell(row, col - 1).value() || '') + "").trim();
    if (!label) {
        label = ((sheet.getCell(row - 1, col).value() || '') + "").trim();
    }
    if (!label) {
        label = `${getLabel(502711,'字段')+fieldIndex}`;
    }
    if (!this.labels) {
        this.labels = {};
    }
    let currentLabels = this.labels[sheet.name()];
    if (!currentLabels) {
        currentLabels = [];
        this.labels[sheet.name()] = currentLabels;
    }
    while (currentLabels.indexOf(label) > -1) {
        label = `${label}${fieldIndex}`;
    }
    currentLabels.push(label);
    return label;
}
export function getDetailIndex(mainTable) {
    if (this.detailIndex[mainTable] !== void 0) {
        this.detailIndex[mainTable] = this.detailIndex[mainTable] + 1;
    } else {
        this.detailIndex[mainTable] = 1;
    }
    return this.detailIndex[mainTable];
}
export function getFieldIndex(mainTable, detail) {
    let index = get(this.fieldIndex, `${mainTable}.${detail}`);
    if (!index) {
        index = 1;
    } else {
        index++;
    }
    set(this.fieldIndex, `${mainTable}.${detail}`, index);
    return index;
}
export function getFieldName(mainTable, detail, fieldName, index = 0) {
    const array = get(this.fieldName, `${mainTable}.${detail}`) || [];
    let newFieldName = `${fieldName}${index > 0 ? index : ''}`;
    while (array.indexOf(newFieldName) > -1) {
        index = index + 1;
        newFieldName = `${fieldName}${index > 0 ? index : ''}`;
    }
    array.push(newFieldName);
    set(this.fieldName, `${mainTable}.${detail}`, array);
    return newFieldName;
}

